import { _decorator, Button, Component, instantiate, Node, Prefab } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('GameCode')
export class GameCode extends Component {
    
    @property({ type: Prefab })                     //被选择放置的炮台预制体 Prefabs
    private ships_biomech: Prefab = null; // 预制体1
    @property({ type: Prefab })
    private ships_frist: Prefab = null; // 预制体2
    @property({ type: Prefab })
    private ships_human: Prefab = null; // 预制体3
    @property({ type: Prefab })
    private ships_saucer: Prefab = null; // 预制体4
    @property({ type: Prefab })
    private ships_void: Prefab = null; // 预制体5


    @property({ type: Node })                       //放置炮台的空节点 player
    private emptyNode1: Node = null; // 空节点1
    @property({ type: Node })
    private emptyNode2: Node = null; // 空节点2
    @property({ type: Node })
    private emptyNode3: Node = null; // 空节点3
    @property({ type: Node })
    private emptyNode4: Node = null; // 空节点4
    @property({ type: Node })
    private emptyNode5: Node = null; // 空节点5

    @property(Node)
    btnInner_0:Node=null;
    @property(Node)
    btnInner_1:Node=null;
    @property(Node)
    btnInner_2:Node=null;
    @property(Node)
    btnInner_3:Node=null;
    @property(Node)
    btnInner_4:Node=null;

    @property(Node)
    btnOuter_0:Node=null;
    @property(Node)
    btnOuter_1:Node=null;
    @property(Node)
    btnOuter_2:Node=null;
    @property(Node)
    btnOuter_3:Node=null;
    @property(Node)
    btnOuter_4:Node=null;


    @property(Button)
    tolls_Btn:Button=null;

    @property(Node)
    tower_ScrollView:Node=null;
    @property(Node)
    tolls_ScrollView:Node=null;

    private isTollsVisible:boolean=true;


    private selectedButton: number = -1; // 选中的按钮索引

    start() {
        // 为外部按钮添加点击事件
        this.btnOuter_0.on('click', () => {
            this.selectedButton = 1; // 选中按钮1
            this.emptyNode1.destroyAllChildren();
        });

        this.btnOuter_1.on('click', () => {
            this.selectedButton = 2; // 选中按钮2
            this.emptyNode2.destroyAllChildren();
        });
        this.btnOuter_2.on('click', () => {
            this.selectedButton = 3; // 选中按钮3
            this.emptyNode3.destroyAllChildren();
        });
        this.btnOuter_3.on('click', () => {
            this.selectedButton = 4; // 选中按钮4
            this.emptyNode4.destroyAllChildren();
        });
        this.btnOuter_4.on('click', () => {
            this.selectedButton = 5; // 选中按钮5
            this.emptyNode5.destroyAllChildren();
        });



        // 为ScrollView内的按钮添加点击事件
        this.btnInner_0.on('click', () => {
            this.placePrefab(this.ships_biomech);
        });

        this.btnInner_1.on('click', () => {
            this.placePrefab(this.ships_frist);
        });
        this.btnInner_2.on('click', () => {
            this.placePrefab(this.ships_human);
        });

        this.btnInner_3.on('click', () => {
            this.placePrefab(this.ships_saucer);
        });
        this.btnInner_4.on('click', () => {
            this.placePrefab(this.ships_void);
        });
    }

    private placePrefab(prefab: Prefab) {
        if (this.selectedButton === -1) return; // 如果没有选中按钮，直接返回

        let targetNode = this.selectedButton === 1 ? this.emptyNode1 :this.selectedButton === 2 ? this.emptyNode2 :this.selectedButton === 3 ? this.emptyNode3 :this.selectedButton === 4 ? this.emptyNode4 : this.emptyNode5;
        let newNode = instantiate(prefab); // 实例化预制体
        targetNode.destroyAllChildren();
        targetNode.addChild(newNode); // 将预制体放置到对应的空节点内
    }
    onTools_BtnClick(){
        if(this.tower_ScrollView){
            this.tower_ScrollView.active=!this.isTollsVisible;
        }
        if(this.tower_ScrollView){
            this.tolls_ScrollView.active=this.isTollsVisible;
        }
        this.isTollsVisible=!this.isTollsVisible;
    }
}